//
//  TodayHomeCellBaseViewModel.swift
//  Alamofire
//
//  Created by queue on 2022/7/22.
//

import Foundation
import RxCocoa
import RxSwift
import MYModule


open class HomeBaseViewModel: ViewModel {
    
    public let cellType: HomeCellType
    
    /// 日期变更
    public let dateReplay = BehaviorRelay<Date?>(value: nil)
    
    /// 刷新数据
    public let reloadDataSubject = PublishSubject<HomeBaseViewModel>()
    
    /// 连接设备
    public let hasDevicedReplay = BehaviorRelay<Bool>(value: false)
    
   
    public init(cellType: HomeCellType) {
        
        self.cellType = cellType
        
        super.init()
        
        dateReplay.subscribe(onNext: {[weak self] in
//            debugPrint( "class: \(self?.classForCoder)")
            self?.reloadData($0)
            
            guard let self = self else {
                
                return
                
            }
            let hasDevice = BluetoothManager.shared.deviceState == .connected
            self.hasDevicedReplay.accept(hasDevice)
            self.reloadDataSubject.onNext(self)
            
        }).disposed(by: rx.disposeBag)
        
     
        
    }
    
    
    /// 根据日期刷新数据
    /// - Parameter date: 日期
    public func reloadData(_ date: Date? = nil) {
        
    }
    

    public static func dateStr(date: Date) ->String {
        
        return date.localStringOfDateTime
        
    }
    
    /// 全天数据与单次数据日期选择
    /// - Parameters:
    ///   - fullDayDate: 全天数据日期
    ///   - singleDate: 单次数据日期
    /// - Returns: 显示的日期
    open func lastDate(fullDayDate: Date?, singleDate: Date?) ->Date? {
        var fullDayTime: Double = 0
        var singleTime: Double = 0
        if let fullDayDate = fullDayDate {
            fullDayTime = fullDayDate.timeIntervalSince1970
        }
        
        if let singleDate = singleDate {
            singleTime = singleDate.timeIntervalSince1970
        }
        
        if fullDayTime == 0 && singleTime == 0 {
            return nil
        }
        
        if singleTime == 0 {
            return fullDayDate
        }
        
        if fullDayTime == 0 {
            return singleDate
        }
        
        if singleTime > fullDayTime {
            return singleDate
        }
        
        return fullDayDate

    }
    
    
}


